home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4950 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: geraldo.cc.utexas.edu!usenet
  2. From: Richard Kilgore <rkilgore@lore.ece.utexas.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: So I have this pointer-to-member function...
  5. Date: 1 Feb 1996 19:18:03 GMT
  6. Organization: The University of Texas at Austin, Austin, Texas
  7. Message-ID: <4er3lb$g63@geraldo.cc.utexas.edu>
  8. References: <d4c1.smail.smayo@tiac.net> <310BD244.331C@gsfc.nasa.gov>
  9. NNTP-Posting-Host: lore.ece.utexas.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; Linux 1.3.18 i586)
  14. X-URL: news:310BD244.331C@gsfc.nasa.gov
  15.  
  16. Scott Mayo wrote:
  17. > The compiler will have none of it. It takes one glance at the static struct
  18. > {char*name; void (C::*f)();} list[] = {"A", C::A}; and has a hissy fit at the
  19. > attempt to assign initializers. It's fine outside the class definition; it
  20. > won't work inside.
  21.  
  22. Yes, C++ doesn't allow you to initialize any data members inside the class
  23. declaration, b/c it views any members inside as declarations, not definitions.
  24. Like an extern.  Even if you decided to leave it uninitialized and just set
  25. its contents later somewhere, the linker would complain that it can't find a
  26. definition for "list".
  27.  
  28. I suggest you give the structure a name, and then inside the class declaration
  29. you can use something like:
  30.  
  31.     struct mystruct {char*name; void (C::*f)();};
  32.     struct mystruct list[];
  33.  
  34. And then in your .cc file, you can initialize it with something like:
  35.  
  36.     struct C::mystruct C::list[] = { <whatever> };
  37.  
  38. There might be a shortcut to avoid the struct name if you don't want it, but
  39. as long as you get the idea about the initialization.  No matter what kind of
  40. static data member you have, you'll have to define and initialize it in the
  41. cc file (or .cpp if your a windoze user).
  42.  
  43.     - rick
  44.  
  45. -- 
  46. Richard B. Kilgore
  47. Grad Student, University of Texas at Austin
  48. WWW URL: http://lore.ece.utexas.edu/~rkilgore/
  49. E-mail rkilgore@lore.ece.utexas.edu
  50.  
  51.